package com.tgy.JUC.BlockingQueueDemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/***
 * @ClassName: Test4
 * @Description: 设置超时等待时间
 * @Auther: 送你一块二向箔
 * @Date: 9:58 2022/3/14
 * @version : V1.0
 */
public class Test4 {
    public static void main(String[] args) {
        run1();
        run2();
    }
    public static void run1(){
        ArrayBlockingQueue<String> strings = new ArrayBlockingQueue<>(3);
        try {
            System.out.println(strings.offer("a",5, TimeUnit.SECONDS));
            System.out.println(strings.offer("b",5, TimeUnit.SECONDS));
            System.out.println(strings.offer("c",5, TimeUnit.SECONDS));
            //设置等待超时
            System.out.println(strings.offer("d",5, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    public static void run2(){
        ArrayBlockingQueue<String> strings = new ArrayBlockingQueue<>(3);
        try {
            System.out.println(strings.offer("a",5, TimeUnit.SECONDS));
            System.out.println(strings.poll(2,TimeUnit.SECONDS));
            System.out.println(strings.poll(3,TimeUnit.DAYS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
